<?php
require_once(ML_DATA."/user.php");

class Model {
	public static function update($post_data)
	{
		if (isset($post_data[Protocol::ID])) {
			// On Update
			$data[Protocol::ID]	= $post_data[Protocol::ID];
		}
		else {
			// On Create
			$data[Protocol::USER_ID]	= $post_data[Protocol::USER_ID];
		}
		
		$data[Protocol::MAIN_PHOTO_ID]	= $post_data[Protocol::MAIN_PHOTO_ID];
		$data[Protocol::BIRTH]			= $post_data[Protocol::BIRTH];
		$data[Protocol::GENDER]			= $post_data[Protocol::GENDER];
		$data[Protocol::HEIGHT]			= $post_data[Protocol::HEIGHT];
		$data[Protocol::WEIGHT]			= $post_data[Protocol::WEIGHT];
		$data[Protocol::CHEST]			= $post_data[Protocol::CHEST];
		$data[Protocol::WAIST]			= $post_data[Protocol::WAIST];
		$data[Protocol::HIPS]			= $post_data[Protocol::HIPS];
		$data[Protocol::SHOE]			= $post_data[Protocol::SHOE];
		$data[Protocol::EDUCATIONS]		= $post_data[Protocol::EDUCATIONS];
		$data[Protocol::AGENCIES]		= $post_data[Protocol::AGENCIES];
		$data[Protocol::LOCATIONS]		= $post_data[Protocol::LOCATIONS];
		$data[Protocol::TAGS]			= $post_data[Protocol::TAGS];
		
		$user_data = $post_data;
		$user_data[Protocol::ID]		= $post_data[Protocol::USER_ID];
		$user_data[Protocol::IS_MODEL]	= TRUE;
		
		if (User::update($user_data))
			return DBConnector::update(Protocol::MODEL, $data);
		else return FALSE;
	}
	
	public static function get_list($start = FALSE, $end = FALSE, $keyword = FALSE, $order = FALSE)
	{
		$fields = array(
			Protocol::MODEL.".".Protocol::ID, Protocol::NAME, Protocol::MAIN_PHOTO_ID,
			Protocol::USER.".".Protocol::VIEWS, Protocol::USER.".".Protocol::LIKES,
			Protocol::GENDER, Protocol::HEIGHT, Protocol::WEIGHT, Protocol::CHEST, Protocol::WAIST,
			Protocol::HIPS, Protocol::SHOE, Protocol::LOCATIONS, Protocol::MODEL.".".Protocol::TAGS,
			Protocol::THUMB_B, Protocol::THUMB_S
		);
		$join = "INNER JOIN ".Protocol::USER." ON ".Protocol::USER_ID." = ".Protocol::USER.".".Protocol::ID;
		$join .= " LEFT OUTER JOIN ".Protocol::PHOTO." ON ".Protocol::MAIN_PHOTO_ID." = ".Protocol::PHOTO.".".Protocol::ID;
		$search_fields = array(Protocol::NAME);
		return DBConnector::get_list(Protocol::MODEL, $start, $end, $search_fields, $keyword, $order, $fields, $join);
	}
	
	public static function get($value, $key = FALSE)
	{
		$fields = array(
			Protocol::MODEL.".".Protocol::ID,
			Protocol::USER.".".Protocol::VIEWS, Protocol::USER.".".Protocol::LIKES,
			Protocol::USER.".".Protocol::DESCRIPTION, Protocol::MODEL.".".Protocol::TAGS,
			Protocol::USER.".".Protocol::CREATED_TIME, Protocol::USER.".".Protocol::MODIFIED_TIME,
			Protocol::USER_ID, Protocol::MAIN_PHOTO_ID,
			Protocol::EMAIL, Protocol::PASSWORD, Protocol::NAME,
			Protocol::BIRTH, Protocol::GENDER, Protocol::HEIGHT, Protocol::WEIGHT,
			Protocol::CHEST, Protocol::WAIST, Protocol::HIPS, Protocol::SHOE, Protocol::EDUCATIONS,Protocol::AGENCIES,
			Protocol::LOCATIONS, Protocol::THUMB_B, Protocol::THUMB_S
		);
		$join = "INNER JOIN ".Protocol::USER." ON ".Protocol::USER_ID." = ".Protocol::USER.".".Protocol::ID;
		$join .= " LEFT OUTER JOIN ".Protocol::PHOTO." ON ".Protocol::MAIN_PHOTO_ID." = ".Protocol::PHOTO.".".Protocol::ID;
		
		if ($key === FALSE) $key = Protocol::MODEL.".".Protocol::ID;
		return DBConnector::get(Protocol::MODEL, $key, $value, $fields, $join);
	}
	
	public static function get_by_user_id($user_id)
	{
		return self::get($user_id, Protocol::USER_ID);
	}
	
	public static function delete($id_list)
	{
		/*
		foreach ($id_list as $model_id) {
			$model = Model::get($model_id);
			if (count($model) > 0) {
				$user = User::get($model[Protocol::USER_ID]);
				$user[Protocol::IS_MODEL] = FALSE;
				User::update($user);
			}
		}
		*/
		return DBConnector::delete(Protocol::MODEL, $id_list);
	}
	
	public static function update_career($post_data)
	{
		if (isset($post_data[Protocol::ID])) {
			// On Update
			$data[Protocol::ID]	= $post_data[Protocol::ID];
		}
		else {
			// On Create
			$data[Protocol::MODEL_ID]	= $post_data[Protocol::MODEL_ID];
		}
		
		$data[Protocol::START]	= $post_data[Protocol::START];
		$data[Protocol::END]	= $post_data[Protocol::END];
		$data[Protocol::NAME]	= $post_data[Protocol::NAME];
		
		return DBConnector::update(Protocol::MODEL_CAREER, $data);
	}
	
	public static function get_career_list(
	$model_id, $start = FALSE, $end = FALSE, $keyword = FALSE, $order = FALSE)
	{
		$search_fields = array(Protocol::NAME, Protocol::START, Protocol::END);
		$where = Protocol::MODEL_ID." = $model_id";
		return DBConnector::get_list(
			Protocol::MODEL_CAREER, $start, $end, $search_fields, $keyword, $order, FALSE, FALSE, $where);
	}
	
	public static function delete_career($id_list)
	{
		return DBConnector::delete(Protocol::MODEL_CAREER, $id_list);
	}
}
